//
//  ResizeView.m
//  116114
//
//  Created by shenhaihua on 13-11-13.
//  Copyright (c) 2013年 Raiyi. All rights reserved.
//

#import "ResizeView.h"

@interface ResizeViewInfo () {
    NSMutableArray *_subviews;
}
@property (nonatomic,strong) ResizeView *resizeView;
@property (nonatomic,strong) NSMutableArray *subviews;
@property (nonatomic,strong) NSMutableDictionary *subViewAndKeys;

@end

@implementation ResizeViewInfo
@synthesize view = _view;
@synthesize height = _height;
@synthesize subviews = _subviews;
@synthesize hidden = _hidden;
@synthesize resizeView = _resizeView;
@synthesize subViewAndKeys = _subViewAndKeys;

- (id)init {
    self = [super init];
    if (self) {
        self.view = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return self;
}

- (void)setHidden:(BOOL)hidden {
    if (_height != hidden) {
        _hidden = hidden;
        [self.resizeView reloadSubviews];
    }
}


- (void)addSubview:(UIView *)view {
    if (self.height != self.view.frameHeight) {
        self.view.frameHeight = self.height;
    }
    [_subviews addObject:view];
    [self.view addSubview:view];
}

- (void)addSubview:(UIView *)view viewKey:(NSString *)viewKey {
    if (self.subViewAndKeys == nil) {
        self.subViewAndKeys = [NSMutableDictionary dictionary];
    }
    if (view != nil && viewKey != nil) {
        [self.subViewAndKeys setObject:view forKey:viewKey];
    }
    [self.view addSubview:view];
}

- (UIView *)viewWithKey:(NSString *)viewKey {
    UIView *view = [self.subViewAndKeys objectForKey:viewKey];
    if (view != nil) {
        if (view.superview != self.view) {
            [self.subViewAndKeys removeObjectForKey:viewKey];
            view = nil;
        }
    }
    return view;
}

- (void)cellOnTableView:(ResizeView *)tableView didScrollOnView:(UIView *)view
{
    UIImageView *imageView = (UIImageView*)[self viewWithKey:@"MainImageView"];
    CGRect rectInSuperview = [tableView convertRect:self.view.frame toView:view];
    
    float distanceFromCenter = CGRectGetHeight(view.frame)/2 - CGRectGetMinY(rectInSuperview);
    float difference = CGRectGetHeight(imageView.frame) - CGRectGetHeight(self.view.frame);
    float move = (distanceFromCenter / CGRectGetHeight(view.frame)) * difference;
    
    CGRect imageRect = imageView.frame;
    imageRect.origin.y = -(difference/2)+move;
    imageView.frame = imageRect;
}

@end

@interface ResizeView ()
@property (nonatomic,strong) NSMutableArray *resizeSubviews;


@end

@implementation ResizeView
@synthesize resizeSubviews = _resizeSubviews;
@synthesize minContentHeight = _minContentHeight;
@synthesize yOffset = _yOffset;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.autoresizesSubviews = YES;
        self.resizeSubviews = [NSMutableArray array];
    }
    return self;
}

- (void)addResizeSubview:(ResizeViewInfo *)resizeInfo {
    if (resizeInfo == nil) {
        return;
    }
    [self.resizeSubviews addObject:resizeInfo];
    [self reloadSubviews];
}

- (void)insertResizeSubview:(ResizeViewInfo *)resizeInfo atIndex:(NSInteger)index {
    if (resizeInfo == nil) {
        return;
    }
    [self.resizeSubviews insertObject:resizeInfo atIndex:index];
    [self reloadSubviews];
}

- (NSArray *)resizeSubviewInfos {
    return [NSArray arrayWithArray:self.resizeSubviews];
}

- (void)setMinContentHeight:(CGFloat)minContentHeight {
    _minContentHeight = minContentHeight;
    if (minContentHeight != 0) {
        self.contentSize = CGSizeMake(self.contentSize.width, MAX(minContentHeight, self.contentSize.height));
    }
}

- (void)reloadSubviews {
    CGFloat frameHeight = _yOffset;
    for (ResizeViewInfo *resizeInfo in self.resizeSubviews) {
        resizeInfo.resizeView = self;
        if (resizeInfo.hidden == YES) {
            [resizeInfo.view removeFromSuperview];
        }
        else {
            resizeInfo.view.frame = CGRectMake(0, frameHeight, self.frameWidth, resizeInfo.height);
            [self addSubview:resizeInfo.view];
            frameHeight += resizeInfo.height;
        }
    }
    if (self.scrollEnabled == NO) {
        self.frameHeight = MAX(_minContentHeight, frameHeight);
    }
    else {
        self.contentSize = CGSizeMake(self.contentSize.width, MAX(_minContentHeight, frameHeight));
    }
}

- (void)removeAllResizeViews {
    for (ResizeViewInfo *resizeInfo in self.resizeSubviews) {
        resizeInfo.resizeView = nil;
        [resizeInfo.view removeFromSuperview];
    }
    [self.resizeSubviews removeAllObjects];
    if (self.scrollEnabled == YES) {
        self.contentSize = CGSizeMake(self.contentSize.width, MAX(_minContentHeight, self.frameHeight));
    }
}

@end
